package org.szgb.console;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@ServletComponentScan
@Slf4j
public class ConsoleApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ConsoleApplication.class);
        app.run(args);
    }

    @Bean
    public ApplicationRunner runner(Environment environment) {
        return args -> {
            String port = environment.getProperty("server.port", "8080");
            String contextPath = environment.getProperty("server.servlet.context-path", "");
            String hostAddress = "localhost";

            try {
                hostAddress = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                log.warn("Unable to determine local host", e);
            }

            String swaggerUrl = "http://" + hostAddress + ":" + port + contextPath + "/swagger-ui/index.html";
            String swaggerLocalUrl = "http://localhost:" + port + contextPath + "/swagger-ui/index.html";
            String knife4jUrl = "http://" + hostAddress + ":" + port + contextPath + "/doc.html";
            String knife4jLocalUrl = "http://localhost:" + port + contextPath + "/doc.html";

            log.info("启动成功");
            log.info("Swagger 文档地址: {}", swaggerUrl);
            log.info("Swagger 本地访问地址: {}", swaggerLocalUrl);
            log.info("Knife4j 文档地址: {}", knife4jUrl);
            log.info("Knife4j 本地访问地址: {}", knife4jLocalUrl);
        };
    }

}
